package com.gitee.cui.springcloud.controller;

import javax.annotation.Resource;

import com.gitee.cui.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * TODO
 *
 * @author cuiqingqiang
 * @version 1.0
 * @date 2022-03-07 14:09
 **/
@RestController
@Slf4j
@RequestMapping(value = "/consumer")
// 统一的降级处理逻辑配置
// @DefaultProperties(defaultFallback = "paymentGlobalFallback")
public class OrderHystrixController {

	@Resource
	private PaymentHystrixService paymentHystrixService;

	@GetMapping(value = "/payment/hystrix/ok/{id}")
	public String paymentInfoOk(@PathVariable("id") Long id) {
		return paymentHystrixService.paymentInfoOk(id);
	}

	@GetMapping("/payment/hystrix/timeout/{id}")
	// @HystrixCommand(fallbackMethod = "paymentInfoFallback", commandProperties = {
	// 		@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
	// })
	// @HystrixCommand
	public String paymentInfoTimeout(@PathVariable("id") Long id) {
		// int result = 10 / 0;
		return paymentHystrixService.paymentInfoTimeout(id);
	}

	private String paymentInfoFallback(Long id) {
		return "80客户端调用服务端，服务端超时或报错，触发客户端降级";
	}
	private String paymentGlobalFallback() {
		return "通用的Global降级处理逻辑";
	}
}
